package xin.nick.demo;

import java.util.concurrent.*;

/**
 * @author Nick
 */
public class ThreadCreateDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new MyThread1().start();
        new Thread(new MyRunnable()).start();
        MyCallable myCallable = new MyCallable();
        FutureTask<String> task = new FutureTask<String>(myCallable);
        new Thread(task).start();

        // ---------- 下面是线程池 ---------
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

        for(int i = 0; i < 10; i++) {
            Thread.sleep(10);
            cachedThreadPool.execute(new MyRunnable());
        }
        // 执行完之后它不关闭,手动shutdown
        cachedThreadPool.shutdown();

        int availableProcessors = Runtime.getRuntime().availableProcessors();
        System.out.println("系统可用处理器数" + availableProcessors);
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(availableProcessors);
        for(int i = 0; i < 10; i++) {
            Thread.sleep(10);
            fixedThreadPool.execute(new MyRunnable());
        }
        // 执行完之后它不关闭,手动shutdown
        fixedThreadPool.shutdown();

//        ScheduledExecutorService scheduleThreadPool = Executors.newScheduledThreadPool(4);
//        scheduleThreadPool.scheduleAtFixedRate(new MyRunnable(), 1, 1, TimeUnit.SECONDS);


//        ScheduledExecutorService singleScheduleThreadPool = Executors.newSingleThreadScheduledExecutor();
//        singleScheduleThreadPool.scheduleAtFixedRate(new MyRunnable(), 1, 1, TimeUnit.SECONDS);

        // singleThreadPool 先进先出的好孩子
//        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
//        for(int i = 0; i < 10; i++) {
//            Thread.sleep(10);
//            singleThreadPool.execute(new MyRunnable());
//        }
        // 执行完之后它不关闭,手动shutdown
//        singleThreadPool.shutdown();

        System.out.println(TimeUnit.MILLISECONDS);
        // 自定义线程池
        // -----------------
        // 数组型缓冲等待队列
        BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(10);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 6,
                50, TimeUnit.MILLISECONDS, blockingQueue);

        for (int i = 0; i < 10; i++) {
            threadPoolExecutor.execute(new MyRunnable());
        }
        // 执行完之后它不关闭,手动shutdown
        threadPoolExecutor.shutdown();

        System.out.println("我是主线程");
        System.out.println("子线程的返回值: " + task.get());
    }
}

class MyThread extends Thread {

    @Override
    public void run() {
        try {
            Thread.sleep(Math.round(10));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我是继承Thread的线程");
    }
}

class MyRunnable implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(Math.round(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我是实现Runnable的线程" + Thread.currentThread().getName());
    }
}

class MyCallable implements Callable<String> {
    @Override
    public String call() {
        try {
            Thread.sleep(Math.round(10));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我是实现Callable的线程");
        return "我是call返回的的值";
    }
}